<script setup>
import { ref, reactive, toRaw } from 'vue'
import { service, cache } from '@utils'
import { ElMessage } from 'element-plus'
import TitleValue from '@components/TitleValue'

const emit = defineEmits(['refresh'])

const visible = ref(false)
const formRef = ref()

const account = ref()
const balance = ref()
const state = reactive({
    param: {}
})

const rules = {
    withdrawAmount: [{ required: true, message: '请输入', trigger: 'blur' }]
}

const open = (value, balanceValue) => {
    balance.value = balanceValue
    account.value = value
    visible.value = true
}

const close = () => {
    visible.value = false
    state.param = {}
    account.value = null
    balance.value = null
}

const sure = async () => {
    formRef.value.validate((valid) => {
        if (valid) {
            realSubmit()
        }
    })
}
const realSubmit = async () => {
    const res = await service.fund.withdraw(state.param.withdrawAmount)
    if (res) {
        ElMessage.success('提交成功')
        close()
        emit('refresh')
    }
}
//把方法暴露给父组件
defineExpose({
    open
})
</script>
    
<template>
    <el-dialog v-model="visible" title="提现" center :close-on-click-modal="false" @close="close" destroy-on-close :width="450">
        <section>
            <el-form :model="state.param" :rules="rules" ref="formRef" label-suffix=":" onsubmit="return false;">
                <el-form-item label="提现金额" prop="withdrawAmount">
                    <el-input-number v-model="state.param.withdrawAmount" :controls="false" style="width: 100%" placeholder="请输入数字"
                        :precision="2"/>
                </el-form-item>
            </el-form>
            <div style="padding-left: 80px" class="flex-align marginB10">
                <div class="color-2">可提现余额为{{balance}}元</div>
                <div class="color-theme underline cursor marginL10" @click="state.param.withdrawAmount = balance">全部提现</div>
            </div>
            <section v-if="account">
                <title-value title="收款人" :value="account.payeeName" w="80px"></title-value>
                <title-value title="收款账户" :value="account.payeeAccountNo" w="80px"></title-value>
                <title-value title="账户类型" :value="account.payeeAccountType" w="80px"></title-value>
            </section>
        </section>

        <template #footer>
            <span class="flex-align-end">
                <div class="margin-right20">
                    <el-button @click="close">取消</el-button>
                </div>
                <el-button type="primary" @click="sure">确定</el-button>
            </span>
        </template>
    </el-dialog>
</template>
    